package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xx.job.entity.system.SysMenu;
import com.xx.job.entity.system.SysUser;
import com.xx.job.mapper.SysMenuMapper;
import com.xx.job.service.system.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xx.job.service.system.SysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    public List<SysMenu> getMenuData(HttpServletRequest request){
        Map<String,Object> data = new HashMap<>();
        HttpSession session = request.getSession();
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        List<String> types = new ArrayList<>();
        types.add("M");
        types.add("C");
        List<SysMenu> menuList = getMenuList(0L,sysUser.getRoleId(),types);
        return  menuList;
    }

    @Override
    public List<SysMenu> menulist(String title,String visible) {
        return getMenuList(0L,title,visible);
    }

    @Override
    public SysMenu select(Long id) {
        return sysMenuMapper.selectById(id);
    }

    @Override
    public List<Long> selectByType(List<String> menuTypes) {
        List<Long> menuIds = new ArrayList<>();
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.in("menu_type",menuTypes);
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        sysMenus.forEach(x ->{
            menuIds.add(x.getMenuId());
        });
        return menuIds;
    }



    @Override
    public int delete(Long id) {
        return sysMenuMapper.deleteById(id);
    }

    @Override
    public int update(SysMenu sysMenu) {
        return sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public int add(SysMenu sysMenu) {
        return sysMenuMapper.insert(sysMenu);
    }

    @Override
    public List<Map<String,Object>> menuTreeselect() {
        return getMenuList(0L);
    }


    /**
     * 根据角色权限查询菜单
     * @param parentId
     * @param roleId
     * @return 左边菜单
     */
    @Override
    public List<SysMenu> getMenuList(Long parentId,Long roleId,List<String> types){
        List<SysMenu> menuList = new ArrayList<>();

        List<Long> menuIds = sysRoleMenuService.selectByRoleId(roleId);
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        if(parentId != null){
            wrapper.eq("parent_id",parentId);
        }
        wrapper.in("menu_id",menuIds);
        wrapper.in("menu_type",types);
        wrapper.notIn("paths","/0/63"); // 排除接口权限
        List<SysMenu> sysMenus =  sysMenuMapper.selectList(wrapper);
        if(sysMenus != null && sysMenus.size() != 0){
            for (SysMenu sysMenu : sysMenus) {
                menuList.add(sysMenu);
                List<SysMenu> children = getMenuList(sysMenu.getMenuId(), roleId,types);
                sysMenu.setChildren(children);
            }
        }
        return menuList;
    }

    private List<SysMenu> getMenuList(Long parentId,String title,String visible){
        List<SysMenu> menuList = new ArrayList<>();
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        if(StringUtils.isNotBlank(title)){
            wrapper.like("title",title.trim());
        }
        if(StringUtils.isNotBlank(visible)){
            wrapper.eq("visible",visible);
        }
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        if(sysMenus != null && sysMenus.size() != 0){
            for (SysMenu sysMenu : sysMenus) {
                menuList.add(sysMenu);
                List<SysMenu> children = getMenuList(sysMenu.getMenuId(),title,visible);
                sysMenu.setChildren(children);
            }
        }
        return menuList;
    }

    private  List<Map<String,Object>> getMenuList(Long parentId){
        List<Map<String,Object>> list = new ArrayList<>();
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        wrapper.eq("visible",0);
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        if(sysMenus != null && sysMenus.size() != 0){
            for (SysMenu sysMenu : sysMenus) {
                Map<String,Object> data = new HashMap<>();
                data.put("id",sysMenu.getMenuId());
                data.put("label",sysMenu.getTitle());
                data.put("children",getMenuList(sysMenu.getMenuId()));
                list.add(data);
            }
        }
        return list;
    }

    /**
     * 根据角色权限查询接口
     * @param roleId
     * @return 左边菜单
     */
    @Override
    public List<SysMenu> getMenuList(Long roleId,List<String> types,String path,String method){
        List<SysMenu> menuList = new ArrayList<>();

        List<Long> menuIds = sysRoleMenuService.selectByRoleId(roleId);
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.in("menu_id",menuIds);
        wrapper.in("menu_type",types);
        wrapper.eq("action",method);
        wrapper.eq("path",path);
        List<SysMenu> sysMenus =  sysMenuMapper.selectList(wrapper);
        if(sysMenus != null && sysMenus.size() != 0){
            for (SysMenu sysMenu : sysMenus) {
                menuList.add(sysMenu);
            }
        }
        return menuList;
    }
}
